/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mobexo.android;

import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.Result;
import com.google.android.gcm.server.Sender;
import com.mobexo.codecs.MxoJSONObject;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author hiepch
 */
public class MxoAPNAndroidMessenger {
    private String apiKey;
    private String COLlAPSE_KEY = "Mobexo_Notification";
    private String messageData = null;
    static protected final Logger logger = Logger.getLogger(MxoAPNAndroidMessenger.class.getName());

   

    public MxoAPNAndroidMessenger() {
    }
    
     public MxoAPNAndroidMessenger(String apiKey) {
        this.apiKey = apiKey;
    }
     
      public MxoAPNAndroidMessenger(String apiKey, String messageData) {
        this.apiKey = apiKey;
        this.messageData = messageData;
    }
    
    public boolean send(String deviceId){
        Message message = new Message.Builder().addData("data", messageData).collapseKey(COLlAPSE_KEY).build();
        Sender sender = new Sender(apiKey);
        Result result = null;
        try {
            result =  sender.send(message, deviceId , 1);
        } catch (IOException ex) {
            logger.log(Level.SEVERE, "send {0}", ex);
            return false;
        }
        
        return result != null && result.getMessageId() != null;
    }
    
    public static void main(String[] args) {
		String androidApaKey = "AIzaSyDYNjlx1hEy_hyTUzRedxXvgYAld1z5pg0";
		String registrationId = "APA91bF2D7mXX5ToZ1-wmf10k5U8ODV7nMTBbwXfkJo5-Cae__4eROgPz-4g3Z14NA6_OyR6BSjmuaYZvj8aQSA6G3vNJFDaqli45p905GPQBgopwaml3_SWaRrcL7tvmB11vMui0K9NrrSobeF_GyU3fvcjs4P7Xg";
                MxoAPNAndroidMessenger androidMessenger = new MxoAPNAndroidMessenger(androidApaKey);
                boolean b = androidMessenger.send(registrationId);
                System.out.print(b);
                
    }    
    
}
